﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Service/UserService.cs
// Author: Sohobloo
// CreateTime: 2013/07/01 12:05
// UpdateTime: 2013/07/02 14:23

#endregion

#region 引用

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Sohobloo.SoAuth.Data.Repositories;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoMcCoffee.Public.OperationResult;

#endregion

namespace Sohobloo.SoAuth.Service.Impl
{
    [Export(typeof(IUserService))]
    internal class UserService : ServiceBase, IUserService
    {
        #region 属性

        [Import]
        private IUserRepository UserRepository { get; set; }

        [Import]
        private IPermissionRepository PermissionRepository { get; set; }

        #endregion 属性

        #region IUserService Members

        public OperationResult CreateUser(SoAuthUser user)
        {
            //检查重复
            if(UserRepository.Entities.Any(u => u.UserName == user.UserName))
            {
                return new OperationResult(OperationResultType.Conflict, "用户名重复");
            }
            if(UserRepository.Entities.Any(u => u.Email == user.Email))
            {
                return new OperationResult(OperationResultType.Conflict, "Email重复");
            }
            if(UserRepository.Entities.Any(u => u.NickName == user.NickName))
            {
                return new OperationResult(OperationResultType.Conflict, "昵称重复");
            }

            UserRepository.Insert(user, false);

            return UnitOfWork.Commit() > 0
                ? new OperationResult(OperationResultType.Success)
                : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult DeleteUser(Guid userId)
        {
            PermissionRepository.Delete(p => p.PermissionRole.Id == userId, false);
            var user = UserRepository.GetByKey(userId);

            UserRepository.Delete(user, false);

            return UnitOfWork.Commit() > 0
                ? new OperationResult(OperationResultType.Success)
                : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult UpdateUser(SoAuthUser user)
        {
            //检查重复
            if(UserRepository.Entities.Any(u => u.Id != user.Id && u.UserName == user.UserName))
            {
                return new OperationResult(OperationResultType.Conflict, "用户名重复");
            }
            if(UserRepository.Entities.Any(u => u.Id != user.Id && u.Email == user.Email))
            {
                return new OperationResult(OperationResultType.Conflict, "Email重复");
            }
            if(UserRepository.Entities.Any(u => u.Id != user.Id && u.NickName == user.NickName))
            {
                return new OperationResult(OperationResultType.Conflict, "昵称重复");
            }

            UserRepository.Update(user, false);

            return UnitOfWork.Commit() > 0
                ? new OperationResult(OperationResultType.Success)
                : new OperationResult(OperationResultType.Conflict);
        }

        public ICollection<SoAuthUser> GetUserList()
        {
            return UserRepository.Entities.OrderBy(u => u.CreateTime).ToList();
        }

        public SoAuthUser GetUser(Guid userId)
        {
            return UserRepository.GetByKey(userId);
        }

        public IList<string> QueryUserNameByKeyword(string keyword)
        {
            var startWith = UserRepository.Entities.Where(u => u.UserName.StartsWith(keyword));
            var contains = UserRepository.Entities.Where(u => u.UserName.Contains(keyword) && !u.UserName.StartsWith(keyword));
            var queryResult = startWith.Concat(contains).Select(u => u.UserName).Take(5);
            return queryResult.ToList();
        }

        #endregion
    }
}